package com.yaDuo.common.base

import android.app.Activity
import android.app.Application
import android.content.Context
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.text.HtmlCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.yaDuo.common.util.ToastUtils
import com.yaDuo.common.base.BaseApplication

open class BaseFragment : Fragment() {

    companion object {
        private const val TAG = "BaseFragment"

        protected var mActivity: AppCompatActivity? = null
        private var mFragmentProvider: ViewModelProvider? = null
        private var mActivityProvider: ViewModelProvider? = null
        private var mApplicationProvider: ViewModelProvider? = null


    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as AppCompatActivity
    }

    fun <T : ViewModel> getFragmentScopeViewModel(modelClass: Class<T>): T {
        if (mFragmentProvider == null) {
            mFragmentProvider = ViewModelProvider(this)
        }
        return mFragmentProvider!![modelClass]
    }

    fun <T : ViewModel> getActivityScopeViewModel(modelClass: Class<T>): T {
        if (mActivityProvider == null) {
            mActivityProvider = ViewModelProvider(mActivity!!)
        }
        return mActivityProvider!![modelClass]
    }

    fun <T : ViewModel> getApplicationScopeViewModel(modelClass: Class<T>): T {
        if (mApplicationProvider == null) {
            mApplicationProvider =
                ViewModelProvider(mActivity?.applicationContext as BaseApplication)
        }
        return mApplicationProvider!![modelClass]
    }

    private fun getApplicationFactory(activity: Activity): ViewModelProvider.Factory {
        checkActivity(this)
        val application = checkApplication(activity)
        return ViewModelProvider.AndroidViewModelFactory.getInstance(application)
    }

    private fun checkApplication(activity: Activity): Application {
        val application = activity.application
            ?: throw IllegalStateException(
                "\"Your activity/fragment is not yet attached to \"\n" +
                        "                    + \"Application. You can't request ViewModel before onCreate call.\""
            )
        return application
    }

    private fun checkActivity(fragment: Fragment) {
        val activity = fragment.activity
            ?: throw IllegalStateException("Can't create ViewModelProvider for detached fragment")
    }

    protected fun toast(resId: Int) {
        ToastUtils.show(getString(resId))
    }

    protected fun toast(msg: String) {
        ToastUtils.show(msg)
    }

    fun getHtmlString(@StringRes resId: Int): CharSequence {
        return HtmlCompat.fromHtml(getString(resId), 0)
    }

    fun getHtmlString(@StringRes resId: Int, vararg obj: Any?): CharSequence {
        return HtmlCompat.fromHtml(getString(resId, *obj), 0)
    }
}

